﻿using System.Windows;
using Baosight.ZNH.ZMCommon;
using System;
using Baosight.Platform.Core;
using System.Collections.Generic;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using System.Windows.Controls;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCM11 : EFViewCmes
    {
        private EIBlock departmentBlock;
        
        public ZMCM11()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            base.TempButton = button;

            string status = this.fdDepotStatus.Value == null ? "" : this.fdDepotStatus.Value.ToString();
            if ("停用".Equals(status))
            {
                base.OpenEFWindowAlert("库房停用状态下不能添加新的使用部门！", true);
                return;
            }
            else
            {
                if ("".Equals(status))
                {
                    base.OpenEFWindowAlert("请先选择库房添加部门！", true);
                    return;
                }
                else
                {
                    EIInfo eiInfo = new EIInfo();
                    eiInfo.Set("service", "ZMCM11");
                    eiInfo.Set("method", "regainDepartment");
                   
                    HttpServiceAgent agent = new HttpServiceAgent();
                    agent.CallService(eiInfo, OnDoWorkCompleted);

                    //base.OnInsertButtonClickPopup(new EIInfo(), "部门权限-新增", "insert", null, sender, e);
                }
            }
        }

        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;

            if (inInfo.Status == -1)
            {
                string str = inInfo.GetMsg();
                this.OpenEFWindowAlert(str, true);//弹出提示信息
                return;
            }
            else
            {
                departmentBlock = inInfo.GetBlock("cmes.cm.department");

                //base.OnInsertButtonClickPopup(new EIInfo(), "部门权限-新增", "insert", null, base.TempButton, null);
                OpenPopup(new EIInfo(), "使用部门-新增", "insert", null);
            }
        }
        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            LimitsForDepartment _dept = new LimitsForDepartment(_dialogType);//构造一个新的修改页面
            
            //_eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.department"));//机构下拉列表
            _eIInfo.AddBlock(departmentBlock);

            _dept.EIDataContext = new Platform.Core.UI.EIDataContext();
            _dept.EIDataContext.Update(_eIInfo);

            if (_dialogType == "insert")
            {
                _dept.fdDepotId.Value = this.fdDepotId.Value;
            }

            CmesWindows _popup = new CmesWindows();         //构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;    //设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _dept;                  //让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(PopupWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PopupWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新从表Grid区域
                this.grid.Refresh();
            }
            //按钮生效
            base.TempButtonOpen(true);
        }

        /// <summary>
        /// TAB页签切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        //{
        //    EFTab _tab = sender as EFTab;
        //    if (null != _tab)
        //    {
        //        EFTabItem _item = _tab.SelectedItem as EFTabItem;

        //        if (null != _item && this.LimitsoforPeopleTAB.IsSelected && this.LimitsoforPeopleTAB.Content == null)
        //        {
        //            Dictionary<string, string> _args = new Dictionary<string, string>();
        //            _args.Add("fdDepotId", this.fdDepotId.Value.ToString());
        //            _args.Add("fdDepotStatus", this.fdDepotStatus.Value.ToString());
        //            this.LimitsoforPeopleTAB.LoadView<EFTabItem>("ZMCM12", _args);
        //        }
        //    }
        //}
    }
}
